package com.wu.family.publish;

import java.util.List;

import com.wu.family.R;
import com.wu.family.feed.InstaLoadImageMgr;
import com.wu.family.utils.ze.LoadSDcardImageMgr;
import com.wu.family.utils.ze.LoadSDcardImageMgr.ImageCallBackBm;



import android.R.integer;
import android.content.Context;
import android.graphics.Bitmap;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView.ScaleType;

public class PublishLocalPhotoAdapter extends BaseAdapter {
	private OnItemClickInterface onItemClickInterface;
	public void setOnItemClickInterface(OnItemClickInterface onItemClickInterface) {
		this.onItemClickInterface = onItemClickInterface;
	}

	interface OnItemClickInterface {
		void onPhotoClick(int postion);
		void onBtnClick(int postion, boolean isRecord);
	}
	private List<LocPhotoInfo> photoInfos ; 
	private Context mContext; 
	private final int maxCount;
	public PublishLocalPhotoAdapter(Context context, List<LocPhotoInfo> photoInfos, int maxCount) {
		mContext = context;
		this.photoInfos = photoInfos;
		this.maxCount = maxCount;
	}
	
	@Override
	public int getCount() {
		return photoInfos != null?photoInfos.size():0;
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		ImageView img = (ImageView)arg1;
		final LocPhotoInfo photoInfo = photoInfos.get(arg0);
		if(img == null) {
			img = new ImageView(mContext);
			img.setScaleType(ScaleType.CENTER_CROP);
		}
		if (arg0 >= maxCount) {
			img.setVisibility(View.INVISIBLE);
		}
		if (photoInfo.getType() == Type.PHOTO) {
			img.setTag(photoInfo.getPath());
			if (PublishActivity.insta) {
				img.setImageBitmap(InstaLoadImageMgr.getInstance().findLocalImage(photoInfo.getPath(), 10));
			}else {
				/*LoadSDcardImageMgr.getInstance()
				.loadDrawble(photoInfo.getPath(), img, null);*/
				LoadSDcardImageMgr.getInstance().LoadImgFromFileNoCache(photoInfo.getPath(), img, 
						new ImageCallBackBm() {
							
							@Override
							public void setImage(Bitmap bitmap, String url, ImageView view) {
								if (((String)view.getTag()).equals(photoInfo.getPath())) {
									view.setImageBitmap(bitmap);
								}
							}
						});
			}
		}else if (photoInfo.getType() == Type.ADDPHOTO) {
			img.setImageResource(R.drawable.addphoto);
		}else if (photoInfo.getType() == Type.RECORD) {
			img.setImageResource(photoInfo.isHasSound()
					?R.drawable.publish_btn_edit:R.drawable.publish_btn_record);
		}
		
		img.setLayoutParams(new Gallery.LayoutParams(
				(int)mContext.getResources().getDimension(R.dimen.galley_high),
				(int)mContext.getResources().getDimension(R.dimen.galley_high)));
		return img;
	}
	enum Type {
		PHOTO,
		RECORD,
		ADDPHOTO;
	}
	static public class LocPhotoInfo {
		private Type type = Type.PHOTO;
		private String path = "";
		private boolean hasSound = false;
		
		public Type getType() {
			return type;
		}
		public void setType(Type type) {
			this.type = type;
		}
		public String getPath() {
			return path;
		}
		public void setPath(String path) {
			this.path = path;
		}
		public boolean isHasSound() {
			return hasSound;
		}
		public void setHasSound(boolean hasSound) {
			this.hasSound = hasSound;
		}
	}
	
	OnItemClickListener onItemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1,
				int arg2, long arg3) {
			if ( arg2 == photoInfos.size()-1) {
				if (photoInfos.get(arg2).getType() == Type.ADDPHOTO) {
					onItemClickInterface.onBtnClick(arg2, false);
				}else if (photoInfos.get(arg2).getType() == Type.RECORD){
					onItemClickInterface.onBtnClick(arg2, true);
				}
			}else {
				if (onItemClickInterface != null) {
					onItemClickInterface.onPhotoClick(arg2);
				}
			}
		}
	};
	
}
